program test_04_human;

{*
Model linking for humanoid with 2 swords and shield
}

uses SysUtils, gl, glu, glut, opengls, md3s, debugs, keyboards, fonts, fpscounters, humanoids, VUMaths;

var 
  Frame : integer;
  xrot, yrot, zrot : real;
  px : integer = 0;
  py : integer = 0;
  pz : integer = -30;
  Humanoid : THumanoid;
  Aloe: TMd3ModelDynamic;
    
procedure OnDisplay; cdecl;
begin
  // OpenGL soap
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity;

  // extra rotation using keyboard
  if KeyIsDown(vkUp) then  xrot := xrot + 5;
  if KeyIsDown(vkDown) then xrot := xrot - 5;
  if KeyIsDown(vkLeft) then  zrot := zrot + 5;
  if KeyIsDown(vkRight) then zrot := zrot - 5;
  if KeyIsDown(vkEsc) then halt;
  if KeyTrigger(vkSmallW) then Humanoid.Lower.SetAnimation('WALK');
  if KeyTrigger(vkSmallC) then Humanoid.Lower.SetAnimation('CROUCH');

  if KeyIsDown(vkSmallW) then  py := py + 1;
  if KeyIsDown(vkSmallS) then  py := py - 1;

  // camera
  glTranslatef(px,py,pz);
  glRotatef(xrot,1.0,0.0,0.0);
  glRotatef(yrot,0.0,1.0,0.0);
  glRotatef(zrot,0.0,0.0,1.0);

  // update time
  frame := frame + 30;
  
  // show model
  Humanoid.Draw(frame/1000);

  // print FPS
  Fps.Tic;
  Font.OutTextXY(10,10,'FPS: '+inttostr(Fps.Effective));

  // refresh
  glutSwapBuffers;
  Sleep(1000 div 20);
end;

procedure OnIdle; cdecl;
begin
  glutPostRedisplay;
end;

begin
  // OpenGL stuff  
  InitOpenGl;
  Font := TFont.Create('data/font/dejavu_mono.png');
  Font.SetColor(0,1,0);
  Fps.Limit := 20;

  // initial rotation
  xrot := 0;
  yrot := 0;
  zrot := 0;
  
  // create model
  Humanoid := THumanoid.Create('data/model/human/male/','default');
  Humanoid.RightWeapon := Humanoid.CreateEquipment(Humanoid.Upper, 'tag_right_hand',  'tag_handle', 'data/model/weapon/sword/', 'basic',    'default', 'IDLE');
  Humanoid.LeftWeapon :=  Humanoid.CreateEquipment(Humanoid.Lower, 'tag_left_case',   'tag_handle', 'data/model/weapon/sword/', 'claymore', 'default', 'IDLE');
  Humanoid.Shield :=      Humanoid.CreateEquipment(Humanoid.Upper, 'tag_left_shield', 'tag_handle', 'data/model/shield/round/', 'round',    'default', 'IDLE'); 
  Humanoid.CalculateEyesMatrix;
  
  // let grow some extra aloe from humanoids back
//  Aloe := Humanoid.CreateEquipment(Humanoid.Upper, 'tag_back', 'tag_ground', 'data/model/plant/aloe/', 'aloe', 'default', 'IDLE');

  // info  
  writeln;
  writeln('Use arrow keys LEFT, RIGHT, UP, DOWN to rotate platform');
  writeln('Press Enter to continue, Esc to exit');
  writeln;

  // start loops
  glutDisplayFunc(@OnDisplay);
  glutIdleFunc(@OnIdle);

  // main loop
  glutMainLoop;
end.
